home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ohlutil.zip / CP-HASH.C < prev    next >
C/C++ Source or Header  |  1990-05-19  |  6KB  |  218 lines

  1. /*  cp-hash.c  -- file copying (hash search routines)
  2.     Copyright (C) 1989, 1990 Free Software Foundation.
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 1, or (at your option)
  7.     any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.     Written by Torbjorn Granlund, Sweden (tege@sics.se). */
  19.  
  20. #include <stdio.h>
  21. #include "cp.h"
  22.  
  23. char *hash_insert ();
  24. char *hash_insert2 ();
  25.  
  26. struct htab *htab;
  27. char new_file;
  28.  
  29. /* Add PATH to the list of files that we have created.
  30.    Return 0 if successful, 1 if not. */
  31.  
  32. int
  33. remember_created (path)
  34.      char *path;
  35. {
  36.   struct stat sb;
  37.  
  38.   if (stat (path, &sb) < 0)
  39.     {
  40.       error (0, errno, "%s", path);
  41.       return 1;
  42.     }
  43.  
  44.   hash_insert (sb.st_ino, sb.st_dev, &new_file);
  45.   return 0;
  46. }
  47.  
  48. /* Add path NODE, copied from inode number INO and device number DEV,
  49.    to the list of files we have copied.
  50.    Return NULL if inserted, otherwise non-NULL. */
  51.  
  52. char *
  53. remember_copied (node, ino, dev)
  54.      char *node;
  55.      ino_t ino;
  56.      dev_t dev;
  57. {
  58.   return hash_insert (ino, dev, node);
  59. }
  60.  
  61. /* Allocate space for the hash structures, and set the global
  62.    variable `htab' to point to it.  The initial hash module is specified in
  63.    MODULUS, and the number of entries are specified in ENTRY_TAB_SIZE.  (The
  64.    hash structure will be rebuilt when ENTRY_TAB_SIZE entries have been
  65.    inserted, and MODULUS and ENTRY_TAB_SIZE in the global `htab' will be
  66.    doubled.)  */
  67.  
  68. void
  69. hash_init (modulus, entry_tab_size)
  70.      unsigned modulus;
  71.      unsigned entry_tab_size;
  72. {
  73.   struct htab *htab_r;
  74.  
  75.   htab_r = (struct htab *)
  76.     xmalloc (sizeof (struct htab) + sizeof (struct entry *) * modulus);
  77.  
  78.   htab_r->entry_tab = (struct entry *)
  79.     xmalloc (sizeof (struct entry) * entry_tab_size);
  80.  
  81.   htab_r->modulus = modulus;
  82.   htab_r->entry_tab_size = entry_tab_size;
  83.   htab = htab_r;
  84.  
  85.   forget_all ();
  86. }
  87.  
  88. /* Reset the hash structure in the global variable `htab' to
  89.    contain no entries.  */
  90.  
  91. void
  92. forget_all ()
  93. {
  94.   int i;
  95.   struct entry **p;
  96.  
  97.   htab->first_free_entry = 0;
  98.  
  99.   p = htab->hash;
  100.   for (i = htab->modulus; i > 0; i--)
  101.     *p++ = NULL;
  102. }
  103.  
  104. /* Insert path NODE, copied from inode number INO and device number DEV,
  105.    into the hash structure in the global variable `htab', if an entry with
  106.    the same inode and device was not found already.
  107.    Return NULL if inserted, otherwise non-NULL. */
  108.  
  109. char *
  110. hash_insert (ino, dev, node)
  111.      ino_t ino;
  112.      dev_t dev;
  113.      char *node;
  114. {
  115.   struct htab *htab_r = htab;
  116.  
  117.   if (htab_r->first_free_entry >= htab_r->entry_tab_size)
  118.     {
  119.       int i;
  120.       struct entry *ep;
  121.       unsigned modulus;
  122.       unsigned entry_tab_size;
  123.  
  124.       /* Increase the number of hash entries, and re-hash the data.
  125.      The method of shrinking and increasing is made to compactify
  126.      the heap.  If twice as much data would be allocated
  127.      straightforwardly, we would never re-use a byte of memory.  */
  128.  
  129.       /* Let htab shrink.  Keep only the header, not the pointer vector.  */
  130.  
  131.       htab_r = (struct htab *)
  132.     xrealloc ((char *) htab_r, sizeof (struct htab));
  133.  
  134.       modulus = 2 * htab_r->modulus;
  135.       entry_tab_size = 2 * htab_r->entry_tab_size;
  136.  
  137.       /* Increase the number of possible entries.  */
  138.  
  139.       htab_r->entry_tab = (struct entry *)
  140.     xrealloc ((char *) htab_r->entry_tab,
  141.           sizeof (struct entry) * entry_tab_size);
  142.  
  143.       /* Increase the size of htab again.  */
  144.  
  145.       htab_r = (struct htab *)
  146.     xrealloc ((char *) htab_r,
  147.           sizeof (struct htab) + sizeof (struct entry *) * modulus);
  148.  
  149.       htab_r->modulus = modulus;
  150.       htab_r->entry_tab_size = entry_tab_size;
  151.       htab = htab_r;
  152.  
  153.       i = htab_r->first_free_entry;
  154.  
  155.       /* Make the increased hash table empty.  The entries are still
  156.      available in htab->entry_tab.  */
  157.  
  158.       forget_all ();
  159.  
  160.       /* Go through the entries and install them in the pointer vector
  161.      htab->hash.  The items are actually inserted in htab->entry_tab at
  162.      the position where they already are.  The htab->coll_link need
  163.      however be updated.  Could be made a little more efficient.  */
  164.  
  165.       for (ep = htab_r->entry_tab; i > 0; i--)
  166.     {
  167.       hash_insert2 (htab_r, ep->ino, ep->dev, ep->node);
  168.       ep++;
  169.     }
  170.     }
  171.  
  172.   return hash_insert2 (htab_r, ino, dev, node);
  173. }
  174.  
  175. /* Insert path NODE, copied from inode number INO and device number DEV,
  176.    into the hash structure HTAB, if not already present.
  177.    Return NULL if inserted, otherwise non-NULL. */
  178.  
  179. char *
  180. hash_insert2 (htab, ino, dev, node)
  181.      struct htab *htab;
  182.      ino_t ino;
  183.      dev_t dev;
  184.      char *node;
  185. {
  186.   struct entry **hp, *ep2, *ep;
  187.   hp = &htab->hash[ino % htab->modulus];
  188.   ep2 = *hp;
  189.  
  190.   /* Collision?  */
  191.  
  192.   if (ep2 != NULL)
  193.     {
  194.       ep = ep2;
  195.  
  196.       /* Search for an entry with the same data.  */
  197.  
  198.       do
  199.     {
  200.       if (ep->ino == ino && ep->dev == dev)
  201.         return ep->node;    /* Found an entry with the same data.  */
  202.       ep = ep->coll_link;
  203.     }
  204.       while (ep != NULL);
  205.  
  206.       /* Did not find it.  */
  207.  
  208.     }
  209.  
  210.   ep = *hp = &htab->entry_tab[htab->first_free_entry++];
  211.   ep->ino = ino;
  212.   ep->dev = dev;
  213.   ep->node = node;
  214.   ep->coll_link = ep2;        /* ep2 is NULL if not collision.  */
  215.  
  216.   return NULL;
  217. }
  218.